
--[[
	登录流程

--]]
local Ltui = require("ltui")
local Dialog   = Ltui.dialog
local Panel = Ltui.panel
local Rect = Ltui.rect
local Button = Ltui.button

local cjson = require("cjson")
---------------------------------------------------------
-- class
---------------------------------------------------------
local Login = class("Login")

function Login:ctor(app)
    self.app = app
    self.client_net = new("client_net")
    self.timer = new("timer")
    --连接gate
    local ip = "127.0.0.1"
    local port = 60022
    self.client_net:connect(ip, port)

    self.dialog = Dialog:new("dialog_login", Rect:new(0, 0, 100, 50), ""):background_set("red")
    app:insert(self.dialog)
    local text = "close"
    local btn = Button:new("btn", Rect:new(self.dialog:width() - 10, 2, #text, 1), text, function()
        self.dialog:quit()
    end)
    self.dialog:insert(btn)


    local btn = Button:new("btn_login", Rect:new(self.dialog:width()/2, self.dialog:height()/2, 10, 1), "login", function()
        self:reqLogin()
    end)
    self.dialog:insert(btn)



    self:register()
end


function Login:register()
    g.dispatch:register("games.RES_PING", handler(self, self.pingRes))
    g.dispatch:register("games.RES_LOGIN", handler(self, self.resLogin))
end

function Login:onStart()

    --连接gate
    local ip = "127.0.0.1"
    local port = 60022
    local res = self.client_net:connect(ip, port)
    if not res then
        return
    end

    --定时器
    --self.timer:start({sec=3, callback=Login(self, self.pingReq)} )
    --self.timer:start({sec=3, callback=Login(self, self.reqLogin)} )
    --self.timer:start({sec=5, callback=Login(self, self.pingReq)} )
end


function Login:pingReq( ... )
    -- body
    local data = {
        time = os.time(),
    }
    self.client_net:send("games.REQ_PING", data)
end

function Login:pingRes(data)

    print("pingRes:", data)
end

--登录请求
function Login:reqLogin( ... )
    local file_path = "/data/client_data.json"
    local record = load_json_file(file_path)
    local account_type = "gw_token"
    if not record.aid or not record.token or record.token == "" then
        account_type = "gw_account"
        record.extra = {
            name = "test",
            password = "123456",
        }
    end
    local data = {
        account_type = account_type,
        aid = record.aid,
        openid = record.openid or "",
        token = record.token or "",
    }
    if record.extra then
        data.extra = cjson.encode(record.extra)
    else
        record.extra = ""
    end
    --print("record:",data)
    self.client_net:send("games.REQ_LOGIN", data)
end

--登录返回
function Login:resLogin(data)
    --print("resLogin data:", data)
    --self.dialog:quit()
    local file_path = "/data/client_data.json"
    local record = {}
    if data.error_code == 0 then
        --print("111")
        --登录成功
        record.token = data.token
        record.aid = data.aid
        --保存下token
        save_file(file_path, cjson.encode(record))
        return
    end
    if data.error_code ~= 0 then
        --print("222")
        --token 过期
        record = load_json_file(file_path)
        record.token = ""
        print("resLogin record:",record)
        save_file(file_path, cjson.encode(record))
        return
    end
    return
end












return Login